/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Fenetre;

import Fenetre.BoutonListener.BouttonListenerGenerer;
import Fenetre.BoutonListener.BouttonListenerModifier;
import Fenetre.BoutonListener.ButtonListenerAjouterDate1;
import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import modele.BonDeCommande;
import modele.BonEnSaisie;
import org.joda.time.DateTime;

/**
 *
 * @author gerard.dl02
 */
public class FenetreResultatRecherche extends JFrame {

    private final ArrayList<BonDeCommande> listeBdC;
    private JComboBox selectionBdC;
    private JCheckBox siteAngers;
    private JCheckBox siteNantes;
    private JCheckBox siteRennes;
    private JTextField raisonSociale;
    private JTextField adresse;
    private JTextField codePostal;
    private JTextField ville;
    private JTextField nomSignataire;
    private JTextField nomIntervenant;
    private JTextField telIntervenant;
    private JTextField mailIntervenant;
    private JPanel panelListeDates;
    private JList listeDates;
    private JTextField jour;
    private JTextField mois;
    private JTextField annee;
    private JRadioButton journee;
    private JRadioButton matinee;
    private JRadioButton apresmidi;
    private JTextField designation;
    private ComboBoxSessions session;
    private JTextField PrixUnitaire;
    private JCheckBox matin;
    private JCheckBox apremMidi;
    private JCheckBox cheque;
    private JCheckBox virement;
    private JTextField observation;
    private JTextField delMin;
    private JTextField delMax;
    private JTextField objectifIntervention;
    private ArrayList<DateTime> dates;
    
    
    public FenetreResultatRecherche(ArrayList listeBdC){
        
        this.listeBdC = listeBdC;
        this.setSize(800, 800);
        this.setTitle("Resultat");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setContentPane(creationPanelRecherche());

        this.setVisible(true);
    }

    private void selectionBdC() {

        String tabBdC[] = new String[this.listeBdC.size()];
        int nb = 0;
        for (BonDeCommande bon : listeBdC) {
            String site;
            switch (bon.getSite()) {
                case BonEnSaisie.SITE_ANGERS:
                    site = "A";
                    break;
                case BonEnSaisie.SITE_NANTES:
                    site = "N";
                    break;
                case BonEnSaisie.SITE_RENNES:
                    site = "R";
                    break;
                default:
                    site = " ";
            }
            tabBdC[nb] = site + bon.getDates().get(0).getYear() + "/" + bon.getDates().get(0).getMonthOfYear() + "/" + bon.getNumero();
        }
        this.selectionBdC = new JComboBox(tabBdC);
        selectionBdC.addActionListener(session);
    } 
    
    private JPanel commande(){
        
        
        this.jour = new JTextField(2);
        this.mois = new JTextField(2);
        this.annee = new JTextField(4);
        this.journee = new JRadioButton("Journée");
        this.journee.setSelected(true);
        this.matinee = new JRadioButton("Matinée");
        this.apresmidi = new JRadioButton("Après-midi");
        this.PrixUnitaire = new JTextField(5);
        this.designation = new JTextField(8);
        try {
        this.session = new ComboBoxSessions();
        } catch (Exception e){
            new FenetreException(e);
        }
        
        JButton boutonDate = new JButton("Ajouter à la liste");
        boutonDate.addActionListener(new ButtonListenerAjouterDate1(this));
     
        JPanel panelDate = new JPanel();
        this.panelListeDates = new JPanel();
        JPanel panelTextField = new JPanel();
        JPanel panelCommande = new JPanel();
        

        
        panelTextField.add(new JLabel("Designation : "));
        panelTextField.add(designation);
        panelTextField.add(new JLabel("Session : "));
        panelTextField.add(session);
        panelTextField.add(new JLabel("Prix unitaire H.T. : "));
        panelTextField.add(PrixUnitaire);
       
        
        panelDate.add(new JLabel("Date"));
        panelDate.add(jour);
        panelDate.add(new JLabel("/"));
        panelDate.add(mois);
        panelDate.add(new JLabel("/"));
        panelDate.add(annee);
        panelDate.add(journee);
        panelDate.add(matinee);
        panelDate.add(apresmidi);
        panelDate.add(boutonDate);
        
        panelListeDates.add(new JLabel("Dates des cours : "));
 
        
        panelCommande.add(panelDate);
        panelCommande.add(this.panelListeDates);
        panelCommande.add(panelTextField);
        
        return panelCommande;

    }
    public void panelListeDates(){
        
        String tabDates[] = new String[this.dates.size()];
        int nb = 0;
        for (DateTime date : this.dates){
            tabDates[nb] = date.getDayOfMonth()+"/"+date.getMonthOfYear()+"/"+date.getYear();
            nb++;
        }
        JList liste = new JList(tabDates);
        liste.setVisibleRowCount(5);
        if (this.listeDates != null) {
        this.panelListeDates.remove(this.listeDates);
        }
        this.listeDates = liste;
        this.panelListeDates.add(liste);
        this.revalidate();
        this.repaint();
    }
    
    private JPanel pedagogie(){
        
        JLabel labelInformations = new JLabel("Informations pédagogiques : ");
        this.objectifIntervention = new JTextField(20);
        
        JPanel panelPedagogie = new JPanel();
        
        panelPedagogie.add(labelInformations);
        panelPedagogie.add(objectifIntervention);
        
        return panelPedagogie;
        
    }
    
    
    
    private JPanel horaireDesCours(){
        
       JLabel labelHoraires = new JLabel("Horaires : "); 
       this.matin = new JCheckBox("09H00 à 12H30 / 13H30 à 17H");
       this.matin.setSelected(true);
       this.apremMidi = new JCheckBox("09H00 à 12H00 / 13H30 à 17H");
       
       JPanel panelHoraireDesCours = new JPanel();
       
       panelHoraireDesCours.add(labelHoraires);
        panelHoraireDesCours.add(matin);
        panelHoraireDesCours.add(apremMidi);
        
        return panelHoraireDesCours;
        
    }

    private JPanel reglementRecherche() {

        JLabel labelDelai = new JLabel("Délai de ");
        JLabel labelA = new JLabel(" à ");
        JLabel labelJours = new JLabel(" Jours.");
        JLabel labelMoyensReglement = new JLabel("Moyens de règlement: ");
        JLabel labelObservations = new JLabel("Observations : ");

        JPanel panelReglement = new JPanel();
        JPanel panelDelai = new JPanel();
        JPanel panelObservation = new JPanel();
        JPanel panReglement = new JPanel();

        ButtonGroup groupeReglement = new ButtonGroup();
        groupeReglement.add(cheque);
        groupeReglement.add(virement);

        panelObservation.add(labelObservations);
        panelObservation.add(observation);

        panelReglement.add(labelMoyensReglement);
        panelReglement.add(cheque);
        panelReglement.add(virement);

        panelDelai.add(labelDelai);
        panelDelai.add(delMin);
        panelDelai.add(labelA);
        panelDelai.add(delMax);
        panelDelai.add(labelJours);

        panReglement.add(panelDelai);
        panReglement.add(panelReglement);
        panReglement.add(panelObservation);

        return panReglement;
    }

    private JPanel boutons() {

        JButton recherche = new JButton("Modifier bon de commande");
        recherche.addActionListener(new BouttonListenerModifier(this));
        
        JButton generer = new JButton("Generer fichier texte");
        generer.addActionListener(new BouttonListenerGenerer(this));
        
        JPanel panel = new JPanel();
        panel.add(recherche);
        panel.add(generer);

        return panel;

    }

    private JPanel prestataire() {

        this.codePostal = new JTextField(10);
        this.ville = new JTextField(10);
        this.raisonSociale = new JTextField(10);
        this.adresse = new JTextField(10);
        this.nomSignataire = new JTextField(10);

        JLabel labelRaisonSocial = new JLabel("Raison sociale");
        JLabel labelAdresse = new JLabel("Adresse");
        JLabel labelCodePostal = new JLabel("Code postal");
        JLabel labelNomSignataire = new JLabel("Nom du signataire");
        JLabel labelVille = new JLabel("Ville");

        JPanel panelRaisonSocial = new JPanel();
        JPanel panelCodePostal = new JPanel();
        JPanel panelNomSignataire = new JPanel();
        JPanel panelSite = new JPanel();
        JPanel panelAdresse = new JPanel();
        JPanel panelVille = new JPanel();
        JPanel panelPrestataire = new JPanel(new FlowLayout());

        panelRaisonSocial.add(labelRaisonSocial);
        panelRaisonSocial.add(raisonSociale);

        panelAdresse.add(labelAdresse);
        panelAdresse.add(adresse);

        panelCodePostal.add(labelCodePostal);
        panelCodePostal.add(codePostal);

        panelNomSignataire.add(labelNomSignataire);
        panelNomSignataire.add(nomSignataire);

        panelVille.add(labelVille);
        panelVille.add(ville);

        panelPrestataire.add(panelSite);
        panelPrestataire.add(panelRaisonSocial);
        panelPrestataire.add(panelAdresse);
        panelPrestataire.add(panelCodePostal);
        panelPrestataire.add(panelVille);
        panelPrestataire.add(panelNomSignataire);

        return panelPrestataire;

    }

    private JPanel Intervenant() {

        this.nomIntervenant = new JTextField(10);
        this.telIntervenant = new JTextField(10);
        this.mailIntervenant = new JTextField(10);

        JPanel panelIntervenant = new JPanel();
        panelIntervenant.add(new JLabel("Nom de l'intervenant : "));
        panelIntervenant.add(nomIntervenant);
        panelIntervenant.add(new JLabel("Tel : "));
        panelIntervenant.add(telIntervenant);
        panelIntervenant.add(new JLabel("Email : "));
        panelIntervenant.add(mailIntervenant);

        return (panelIntervenant);
    }

    private JPanel rechercheSites() {

        this.siteAngers = new JCheckBox("Angers");
        this.siteNantes = new JCheckBox("Nantes");
        this.siteRennes = new JCheckBox("Rennes");

        JPanel panelSites = new JPanel();
        ButtonGroup sites = new ButtonGroup();

        sites.add(siteAngers);
        sites.add(siteNantes);
        sites.add(siteRennes);

        panelSites.add(new JLabel("Site concerné : "));
        panelSites.add(siteAngers);
        siteAngers.setSelected(true);
        panelSites.add(siteNantes);
        panelSites.add(siteRennes);

        return (panelSites);
    }

    private JPanel creationPanelRecherche() {

        JPanel panel1 = new JPanel();
        BoxLayout box = new BoxLayout(panel1, BoxLayout.PAGE_AXIS);
        panel1.setLayout(box);

        panel1.add(new MenuBar());
        panel1.add(this.selectionBdC);
        panel1.add(rechercheSites());
        panel1.add(prestataire());
        panel1.add(Intervenant());
        panel1.add(commande());
        panel1.add(horaireDesCours());
        panel1.add(pedagogie());
        panel1.add(reglementRecherche());
        panel1.add(this.boutons());

        return panel1;

    }

    public void chargerBdC() {

        BonDeCommande bonSelect = getListeBdC().get(getSelectionBdC().getSelectedIndex());

        switch (bonSelect.getSite()) {
            case BonEnSaisie.SITE_ANGERS:
                this.siteAngers.setSelected(true);
                break;
            case BonEnSaisie.SITE_NANTES:
                this.siteNantes.setSelected(true);
                break;
            case BonEnSaisie.SITE_RENNES:
                this.siteRennes.setSelected(true);
                break;
        }
        this.raisonSociale.setText(bonSelect.getRaisonSociale());
        this.adresse.setText(bonSelect.getAdresse());
        this.codePostal.setText(bonSelect.getCodePostal());
        this.ville.setText(bonSelect.getVille());
        this.nomSignataire.setText(bonSelect.getNomSignataire());
        this.nomIntervenant.setText(bonSelect.getNomIntervenant());
        this.telIntervenant.setText(bonSelect.getTelIntervenant());
        this.mailIntervenant.setText(bonSelect.getEmailIntervenant());
        changerListeDates(bonSelect);
        this.designation.setText(bonSelect.getDesignation());
        this.session.setSelectedItem(bonSelect.getSession());
        this.PrixUnitaire.setText(bonSelect.getPrixUnitaire() + "€");
        switch (bonSelect.getHoraires()) {
            case BonEnSaisie.HORAIRES_12H:
                this.matin.setSelected(true);
                break;
            case BonEnSaisie.HORAIRES_12H30:
                this.apremMidi.setSelected(true);
                break;
        }
        this.objectifIntervention.setText(bonSelect.getInformations());
        this.delMin.setText(bonSelect.getDelaiMinimum()+"");
        this.delMax.setText(bonSelect.getDelaiMaximum()+"");
        switch (bonSelect.getMoyenReglement()) {
            case BonEnSaisie.REGLEMENT_CHEQUE:
                this.cheque.setSelected(true);
                break;
            case BonEnSaisie.REGLEMENT_VIREMENT:
                this.virement.setSelected(true);
                break;
        }
        this.observation.setText(bonSelect.getObservations());

    }

    public void changerListeDates(BonDeCommande bon) {

        String tabDates[] = new String[bon.getDates().size()];
        int nb = 0;
        for (DateTime date : bon.getDates()) {
            String periode = "";
            switch (date.getHourOfDay()) {
                case BonEnSaisie.JOURNEE:
                    periode = " journée";
                    break;
                case BonEnSaisie.MATINEE:
                    periode = " matin";
                    break;
                case BonEnSaisie.APRES_MIDI:
                    periode = " après-midi";
                    break;
            }
            tabDates[nb] = date.getDayOfMonth() + "/" + date.getMonthOfYear() + "/" + date.getYear() + periode;
            nb++;
        }
        JList liste = new JList(tabDates);
        liste.setVisibleRowCount(5);
        if (this.listeDates != null) {
            this.panelListeDates.remove(this.listeDates);
        }
        this.listeDates = liste;
        this.panelListeDates.add(liste);
        this.revalidate();
        this.repaint();
    }

    public JCheckBox getSiteAngers() {
        return siteAngers;
    }

    public JCheckBox getSiteNantes() {
        return siteNantes;
    }

    public JCheckBox getSiteRennes() {
        return siteRennes;
    }

    public JTextField getRaisonSociale() {
        return raisonSociale;
    }

    public JTextField getAdresse() {
        return adresse;
    }

    public JTextField getCodePostal() {
        return codePostal;
    }

    public JTextField getVille() {
        return ville;
    }

    public JTextField getNomSignataire() {
        return nomSignataire;
    }

    public JTextField getNomIntervenant() {
        return nomIntervenant;
    }

    public JTextField getTelIntervenant() {
        return telIntervenant;
    }

    public JTextField getMailIntervenant() {
        return mailIntervenant;
    }

    public JPanel getPanelListeDates() {
        return panelListeDates;
    }

    public JList getListeDates() {
        return listeDates;
    }

    public JTextField getJour() {
        return jour;
    }

    public JTextField getMois() {
        return mois;
    }

    public JTextField getAnnee() {
        return annee;
    }

    public JRadioButton getJournee() {
        return journee;
    }

    public JRadioButton getMatinee() {
        return matinee;
    }

    public JRadioButton getApresmidi() {
        return apresmidi;
    }

    public JTextField getDesignation() {
        return designation;
    }

    public ComboBoxSessions getSession() {
        return session;
    }

    public JTextField getPrixUnitaire() {
        return PrixUnitaire;
    }

    public JCheckBox getMatin() {
        return matin;
    }

    public JCheckBox getApremMidi() {
        return apremMidi;
    }

    public JCheckBox getCheque() {
        return cheque;
    }

    public JCheckBox getVirement() {
        return virement;
    }

    public JTextField getObservation() {
        return observation;
    }

    public JTextField getDelMin() {
        return delMin;
    }

    public JTextField getDelMax() {
        return delMax;
    }

    public JTextField getObjectifIntervention() {
        return objectifIntervention;
    }

    public ArrayList<BonDeCommande> getListeBdC() {
        return listeBdC;
    }

    public JComboBox getSelectionBdC() {
        return selectionBdC;
    }

    public ArrayList getDates() {
        return dates;
    }
    

}
